package tech.wetech.admin.modules.system.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.wetech.admin.modules.base.query.PageQuery;
import tech.wetech.admin.modules.base.service.impl.BaseService;
import tech.wetech.admin.modules.system.mapper.BusHeartMapper;
import tech.wetech.admin.modules.system.po.BusHeart;
import tech.wetech.admin.modules.system.po.SelectTime;
import tech.wetech.admin.modules.system.service.BusHeartService;
import tk.mybatis.mapper.code.Style;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lu
 * @description 公交心跳实现类
 * @date 2019年06月12日 10:19
 */
@Service
public class BusHeartServiceImpl extends BaseService<BusHeart> implements BusHeartService {

    @Autowired
    private BusHeartMapper busHeartMapper;

    /*
    ** @author lu
    * @date 2019/6/13 15:45
    * @param [busHeart, pageQuery, selectTime]
    * @return java.util.List<tech.wetech.admin.modules.system.po.BusHeart>
    * @description公交心跳查询搜索分页
    */
    @Override
    public List<BusHeart> querySelectList(BusHeart busHeart, PageQuery pageQuery, SelectTime selectTime) {
        Example example = new Example(BusHeart.class);
        Example.Criteria criteria = example.createCriteria();
        if (pageQuery.getOrderBy() != null) {
            pageQuery.setOrderBy(StringUtil.convertByStyle(pageQuery.getOrderBy(), Style.camelhump));
        }
        //判断搜索条件是否全部为空，为空不需要进行拼接
        if (busHeart.getPosNo()==null&& selectTime.getStar()==null&&selectTime.getEnd()==null){
            return PageHelper.startPage(pageQuery)
                    .doSelectPage(() -> mapper.select(busHeart));
        }else {
            //判断用户是否输入设备号搜索
            if (busHeart.getPosNo()!=null && selectTime.getStar()==null&&selectTime.getEnd()==null){

                String posNo=busHeart.getPosNo()+"%";
                // 创建条件对象
                criteria.andLike("posNo",posNo);
                return PageHelper.startPage(pageQuery)
                        .doSelectPage(() -> mapper.selectByExample(example));
            }else if(selectTime.getEnd()!=null && busHeart.getPosNo()==null){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date datEnd = null;
                //String posNo=busHeart.getPosNo()+"%";
                // 创建条件对象
                try {
                    datEnd = format.parse(selectTime.getEnd());
                    Date dateStart = format.parse(selectTime.getStar());
                    criteria.andBetween("changeTime",dateStart,datEnd);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return PageHelper.startPage(pageQuery)
                        .doSelectPage(() -> mapper.selectByExample(example));
            }else {
                String posNo=busHeart.getPosNo()+"%";
                // 创建条件对象
                criteria.andLike("posNo",posNo);
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date datEnd = null;
                //String posNo=busHeart.getPosNo()+"%";
                // 创建条件对象
                try {
                    datEnd = format.parse(selectTime.getEnd());
                    Date dateStart = format.parse(selectTime.getStar());
                    criteria.andBetween("changeTime",dateStart,datEnd);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return PageHelper.startPage(pageQuery)
                        .doSelectPage(() -> mapper.selectByExample(example));
            }
        }
    }
}
